//
//  AlamoFireTool.swift
//  ZYChat
//
//  Created by admin on 2019/11/25.
//  Copyright © 2019 admin. All rights reserved.
//

import UIKit
import Alamofire
class AlamoFireTool: NSObject {
    
//    fileprivate static var header: HTTPHeaders = {
//
//        var headrInfo :[String: String] = Dictionary.init();
//        headrInfo.updateValue("application/json", forKey: "Accept")
//
//        if let token = Singleton.shareSingleton.token{
//            headrInfo.updateValue(token, forKey: "token")
//        }else{
//            headrInfo.updateValue(Key.SettingInfo.token, forKey: "token")
//        }
//        if let cid = Singleton.shareSingleton.userID{
//            headrInfo.updateValue(cid, forKey: "cid")
//        }
//
//        headrInfo.updateValue("application/json", forKey: "Accept")
//        return headrInfo
//    }()
    
    static func getHeadr() -> HTTPHeaders  {
           
           var headrInfo :[String: String] = Dictionary.init();
           headrInfo.updateValue("application/json", forKey: "Accept")
           
           if let token = Singleton.shareSingleton.token{
               headrInfo.updateValue(token, forKey: "token")
           }else{
               headrInfo.updateValue(Key.SettingInfo.token, forKey: "token")
           }
           if let cid = Singleton.shareSingleton.userID{
               headrInfo.updateValue(cid, forKey: "cid")
           }
           
           headrInfo.updateValue("application/json", forKey: "Accept")
           return headrInfo
       }
    

    //GET
    static func get(url: URLConvertible,parameters:Parameters?, headers:HTTPHeaders? = getHeadr()) ->DataRequest{
//        let par = encrypt(url: url, parameters: parameters)
        return request(url, method: .get, parameters: parameters, encoding: URLEncoding.default, headers: headers)
    }
    //POST
    static func post(url: URLConvertible,parameters:Parameters?, headers:HTTPHeaders? = getHeadr()) ->DataRequest{
        let par = encrypt(url: url, parameters: parameters)
        return request(url, method: .post, parameters: par, encoding: URLEncoding.default, headers: headers)
    }
    //DELETE
    static func delete(url: URLConvertible,parameters:Parameters?, headers:HTTPHeaders? = getHeadr()) ->DataRequest{
        let par = encrypt(url: url, parameters: parameters)
        return request(url, method: .delete, parameters: par, encoding: URLEncoding.default, headers: headers)
    }
    //PUT
    static func put(url: URLConvertible,parameters:Parameters?, headers:HTTPHeaders? = getHeadr()) ->DataRequest{
        let par = encrypt(url: url, parameters: parameters)
        return request(url, method: .put, parameters: par, encoding: URLEncoding.default, headers: headers)
    }
    //UPLOAD
    //    static func upload(data:Data, url: URLConvertible, headers:HTTPHeaders? = getHeadr()) ->DataRequest{
    //
    //        return Alamofire.upload(data, to: url, method: .post, headers: header)
    //    }
    //加密数据
    static func encrypt(url:URLConvertible, parameters:Parameters?) ->Dictionary<String, Any>{
        ParameterEncrypt.encrypt(paramet: parameters, url: url as! String)
    }
    
    
}
